// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: SendMail.java

package com.market.web.mail;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.market.constant.Constant;
import com.market.web.form.CustomerForm;

// Referenced classes of package com.market.web.mail:
// MailInfo, PasswordAuthenticator

public class SendMail
{

    private static final Logger logger = LoggerFactory.getLogger(SendMail.class.getName());

    public SendMail()
    {
    }

    public List<String> send()
    {
        MailInfo mail = Constant.mail;
        List<String> send = new ArrayList<String>();
        Properties prop = System.getProperties();
        prop.put("mail.smtp.host", mail.getSmtp());
        prop.put("mail.smtp.auth", "true");
        PasswordAuthenticator auth = new PasswordAuthenticator(mail.getUsername(), mail.getPassword());
        Session session = Session.getDefaultInstance(prop, auth);
        Transport tran = null;
        MimeMessage message = new MimeMessage(session);
        MimeMultipart multipart = new MimeMultipart();
        MimeBodyPart bodypart = new MimeBodyPart();
        try
        {
            message.setSubject(title);
            message.setFrom(new InternetAddress(mail.getFrom()));
            message.setSentDate(new Date());
            tran = session.getTransport("smtp");
        }
        catch (Exception e)
        {
            logger.error("", e);
        }
        for (int i = 0; i < customer.size(); i++)
        {
            CustomerForm cus = (CustomerForm)customer.get(i);
            String name = cus.getName();
            boolean sex = cus.isSex();
            String to = cus.getEmail();
            String con = name + (sex ? "\u5148\u751F" : "\u5C0F\u59D0") + content;
            try
            {
                bodypart.setText(con);
                multipart.addBodyPart(bodypart);
                message.setContent(multipart);
                message.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
                Transport.send(message);
                send.add(name);
            }
            catch (Exception e)
            {
                logger.error("", e);
            }
        }

        try
        {
            tran.close();
        }
        catch (Exception e)
        {
            logger.error("", e);
        }
        return send;
    }

    public void addCustomer(CustomerForm cus)
    {
        if (customer == null)
        {
            customer = new ArrayList<CustomerForm>();
        }
        customer.add(cus);
    }

    public String getContent()
    {
        return content;
    }

    public void setContent(String content)
    {
        this.content = content;
    }

    public List<CustomerForm> getCustomer()
    {
        return customer;
    }

    public void setCustomer(List<CustomerForm> customer)
    {
        this.customer = customer;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    private List<CustomerForm> customer;

    private String title;

    private String content;
}
